package com.rightsidetech.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rightsidetech.commom.base.PageRequest;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.enums.SysSignEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.system.dtos.request.SysSignRuleUpReq;
import com.rightsidetech.system.dtos.request.SysSignStatusReq;
import com.rightsidetech.system.dtos.response.SysSignRulePageResp;
import com.rightsidetech.system.entity.SysRuleSignIntegral;
import com.rightsidetech.system.mapper.SysRuleSignIntegralMapper;
import com.rightsidetech.system.service.SysRuleSignIntegralService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.Date;
import java.util.List;

@Service("sysRuleSignIntegralService")
public class SysRuleSignIntegralServiceImpl implements SysRuleSignIntegralService {

    @Resource
    private SysRuleSignIntegralMapper sysRuleSignIntegralMapper;


    @Override
    public ResponseData<PageInfo<SysSignRulePageResp>> page(PageRequest req) {
        PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<SysSignRulePageResp> list = sysRuleSignIntegralMapper.selectList();
        return ResponseData.success(new PageInfo<>(list));
    }

    @Override
    public ResponseData SysSignRuleUpReq(SysSignRuleUpReq req) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        SysRuleSignIntegral ruleSign = sysRuleSignIntegralMapper.selectByPrimaryKey(req.getId());
        if(ObjectUtil.isEmpty(ruleSign)){
            return ResponseData.fail("未找到需要修改的数据");
        }
        ruleSign.setUpdatedAt(new Date());
        ruleSign.setRuleDescribe(req.getRuleDescribe());
        ruleSign.setRuleValue(req.getRuleValue());
        if(ruleSign.getRuleDescribe().length()>50){
            return ResponseData.fail("数据规则描述过长,最长为50位");
        }
        if(ruleSign.getRuleValue().precision()>10){
            return ResponseData.fail("输入规则值有误，最长为10位");
        }
        if (sysRuleSignIntegralMapper.updateByPrimaryKeySelective(ruleSign) <= 0) {
            return ResponseData.fail(CodeIdEnum.FAILED);
        }
        return ResponseData.success(CodeIdEnum.Success);
    }

    @Override
    public SysRuleSignIntegral selectByEnum(SysSignEnum signEnum) {
        return sysRuleSignIntegralMapper.selectByRuleKey(signEnum.getRuleKey());
    }

    @Override
    public ResponseData upStatus(SysSignStatusReq req) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        if (sysRuleSignIntegralMapper.selectCountByStatus(req.getStatus()) > 0) {
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(), "签到规则已经开启，或已经关闭");
        }
        if (sysRuleSignIntegralMapper.updateStatus(req.getStatus()) <= 0) {
            return ResponseData.fail(CodeIdEnum.FAILED);
        }
        return ResponseData.success(CodeIdEnum.Success);
    }
}
